﻿
@{
    ViewBag.Title = "添加新闻";
}
@model KTD.Web.Models.VD.NewsCUD

<div class="x-body" id="saveResult" style="width:100%;">
    <div class="layui-form">
        <div class="layui-form-item">
            <label for="L_email" class="layui-form-label">
                <span class="x-red">*</span>城市
            </label>
            <div class="layui-input-block">
                @Html.DropDownList("cityId", ViewBag.CityList as IEnumerable<SelectListItem>, new { @class = "layui-input", })
            </div>
        </div>
        <div class="layui-form-item">
            <label for="L_email" class="layui-form-label">
                <span class="x-red">*</span>类别
            </label>
            <div class="layui-input-block">
                @Html.DropDownListFor(model => model.pid, ViewBag.desList as IEnumerable<SelectListItem>, new { @class = "layui-input" })
            </div>
        </div>
        <div class="layui-form-item">
            <label for="L_email" class="layui-form-label">
                <span class="x-red">*</span>标题
            </label>
            <div class="layui-input-block">
                @Html.TextBoxFor(model => model.title, new { @class = "layui-input" })
            </div>
        </div>
        <div class="layui-form-item">
            <label for="L_email" class="layui-form-label">
                <span class="x-red">*</span>封面图
            </label>
            <div class="layui-input-block">
                <input type="file" id="file" />
            </div>

        </div>

        <label for="L_email" class="layui-form-label">
            <span class="x-red">*</span>内容
        </label>
        <div class="layui-form-item">


            @Html.TextAreaFor(model => model.content, new { @class = "layui-input", style = "width:100%;height:200px;display:none;" })
            <div id="editor">
                <p>
                    @Html.Raw(HttpUtility.HtmlDecode(Model.content))
                </p>
            </div>
        </div>
        <div class="layui-form-item">
            <label for="L_repass" class="layui-form-label">
            </label>
            <button id="btnAdd" class="layui-btn" lay-filter="add" lay-submit="">
                保存
            </button>
        </div>
    </div>
</div>



@section scripts{
    <script src="~/Scripts/wangEditor.min.js"></script>
    <script>
        $(function () {
            var E = window.wangEditor
            var editor = new E('#editor');
            editor.customConfig.pasteFilterStyle = true;
            editor.customConfig.pasteTextHandle = function (content) {
                // content 即粘贴过来的内容（html 或 纯文本），可进行自定义处理然后返回
                if (content == '' && !content) return ''
                let str = content;
                //处理的标签里的多余代码
                str = str.replace(/<xml>[\s\S]*?<\/xml>/ig, '');

                str = str.replace(/<style>[\s\S]*?<\/style>/ig, '');
                str = str.replace('/(\\n|\\r| class=(")?Mso[a-zA-Z]+(")?)/g', '');
                let reg = new RegExp('<!--(.*?)-->', 'g');
                str = str.replace(reg, '');
                //str = str.replace(/<style>[\s\S]*?<\/style>/ig, '')
                //str = str.replace(/<\/?[^>]*>/g, '')
                // str = str.replace(/[ | ]*\n/g, '\n')
                // str = str.replace(/&nbsp;/ig, '')
                return str
            }
            // 或者 var editor = new E( document.getElementById('editor') )
            editor.create();
            $("#save").click(function () {
                $("#content").val(editor.txt.html());
                return true;
            });
        });
        layui.use(['form', 'layer'], function () {
            $ = layui.jquery;
            var form = layui.form
                , layer = layui.layer;

            //自定义验证规则
            form.verify({
                nikename: function (value) {
                    if (value.length < 5) {
                        return '昵称至少得5个字符啊';
                    }
                }
                , pass: [/(.+){6,12}$/, '密码必须6到12位']
                , repass: function (value) {
                    if ($('#L_pass').val() != $('#L_repass').val()) {
                        return '两次密码不一致';
                    }
                }
            });

            //监听提交
            form.on('submit(add)', function (data) {



                var formdata = new FormData();
                formdata.append("title", $("#title").val());
                formdata.append("des", $("#des").val());
                formdata.append("pid", $("#pid").val());
                formdata.append("content", $("#content").val());
                formdata.append("cityId", $("#cityId").val());
                formdata.append("file", $("#file")[0].files[0]);
                $.ajax({

                    url: "/News/Create",
                    type: "post",
                    data: formdata,
                    processData: false,    // 不处理数据
                    contentType: false,    // 不设置内容类型

                    success: function (data) {

                        //发异步，把数据提交给php
                        layer.alert("增加成功", { icon: 6 }, function () {
                            // 获得frame索引
                            var index = parent.layer.getFrameIndex(window.name);
                            //关闭当前frame
                            parent.layer.close(index);
                            parent.window.location.reload();
                        });
                    }
                })
                return false;
            });


        });

    </script>
}